package com.xbs.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import javax.management.relation.Role;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xbs.controller.base.BaseServlet;
import com.xbs.entity.RoleEntity;
import com.xbs.service.content.RoleAdminService;
/**
 * 删除角色
 * @author 余凯
 *
 */
@WebServlet("/roledel")
public class AjaxRoleDelServlet extends BaseServlet {
	private RoleAdminService rs;
	private List<RoleEntity> relst;
	public AjaxRoleDelServlet() {
		rs = new RoleAdminService();
		relst = new ArrayList<RoleEntity>();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		if(req.getParameter("rid")!=null&&req.getParameter("rid").length()!=0){
			//获取选中的角色的id
			String[] rid = req.getParameter("rid").split(",");
			
			for(int i=0;i<rid.length;i++){
				String role=rs.selR(Integer.parseInt(rid[i]));
				//删除该角色子表的权限
				rs.delChild(Integer.parseInt(rid[i]));
				//删除该角色
				rs.del(Integer.parseInt(rid[i]));
				//清空员工对应的角色
				rs.upemp(role);
			}
		}
	
		relst = rs.selRole();
		String str="<tr>"
				+ "<th>全选<input type='checkbox' class='choice' id='box' onclick='getCheck(id)'/></th>"
				+ "<th>角色名称</th>"
				+ "<th>操作</th>"
				+ "</tr>";
		for(RoleEntity re:relst){
			str+="<tr>"
				+ "<td><input type='checkbox' class='checked' name='ck' onclick='getChild()' value=\""+re.getRoleid()+"\"/></td>"
				+ "<td>"+re.getRolename()+"</td>"
				+ "<td>"
				+ "<input class='update' type='button' value='修 改'  class='pointer' onclick='getName(\""+re.getRolename()+"\")'/>"
				+ "</td>"
				+ "</tr>";
		}
		resp.getWriter().print(str);
	}
}
